package com.example.sherlockzhong.plush_v2.CardBeans;

import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.sherlockzhong.plush_v2.Constants.SystemConstant;
import com.example.sherlockzhong.plush_v2.R;
import com.example.sherlockzhong.plush_v2.View.CardHorizontalScrollView;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by DosonLeungs on 2014/10/3.
 */
public class TodoCardBean {
    private static int cardId = 7;
    private static String cardClass = "Todo";
    private boolean voiceIsPlaying = false;
    private MediaPlayer voicePlayer;
    int todo_icon;
    String todo_title;
    String todo_content;
    String todo_time;
    String todo_type;
    String voice_path;
    String voice_length;

    private ImageButton button = null;

    private int playerLength;
    private Timer playerTimer;
    private TimerTask playerTimerTask;
    private Handler playerHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                stopVoice();
                playerTimer.cancel();
                playerTimerTask.cancel();
                playerTimer = null;
                playerTimerTask = null;
            }
            super.handleMessage(msg);
        }
    };

    public TodoCardBean() {
        this.todo_icon = R.drawable.todo_icon;
        this.todo_title = "";
        this.todo_content = "";
        this.todo_time = "";
        this.todo_type = "";
        this.voice_length = "";
    }

    public static int getCardId() {
        return cardId;
    }

    public static String getCardClass() {
        return cardClass;
    }

    public int getTodoIcon() {
        return todo_icon;
    }

    public void setTodoIcon(int todo_icon) {
        this.todo_icon = todo_icon;
    }

    public String getTodoTitle() {
        return todo_title;
    }

    public void setTodoTitle(String todo_title) {
        this.todo_title = todo_title;
    }

    public String getTodoTime() {
        return todo_time;
    }

    public void setTodoTime(String todo_time) {
        this.todo_time = todo_time;
    }

    public String getTodoContent() {
        return todo_content;
    }

    public void setTodoContent(String todo_content) {
        this.todo_content = todo_content;
    }

    public String getTodo_type() {
        return todo_type;
    }

    public void setTodo_type(String todo_type) {
        this.todo_type = todo_type;
    }

    public String getVoice_path() {
        return voice_path;
    }

    public void setVoice_path(String voice_path) {
        this.voice_path = voice_path;
    }


    public String getVoice_length() {
        return voice_length;
    }

    public void setVoice_length(String voice_length) {
        this.voice_length = voice_length;
    }

    public void insertTo(CardHorizontalScrollView v) {
        ImageView todo_icon = (ImageView) v.findViewById(R.id.todo_icon);
        TextView todo_title = (TextView) v.findViewById(R.id.todo_title);
        TextView todo_content = (TextView) v.findViewById(R.id.todo_content);
        ImageButton todo_record = (ImageButton) v.findViewById(R.id.mediaPlayer);

        todo_icon.setImageResource(this.getTodoIcon());
        todo_title.setText(this.getTodoTitle());
        todo_content.setText(this.getTodoContent());
        if(voice_path!=null){
            if(voice_path!="") {
                todo_content.setText(voice_length);
                todo_record.setVisibility(View.VISIBLE);
                todo_record.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (button == null) {
                            button = (ImageButton) v;
                        }
                        if (voiceIsPlaying) {
                            stopVoice();
                        } else {
                            playVoice(voice_path);
                        }
                    }
                });
            }
        }else{
            //todo_content.setVisibility(View.VISIBLE);
            todo_record.setVisibility(View.INVISIBLE);
        }
    }

    private void playVoice(String path) {
        button.setImageResource(R.drawable.stop);
        voiceIsPlaying = true;
        voicePlayer = new MediaPlayer();
        SystemConstant.voicePlayer = voicePlayer;
        playerLength = Integer.parseInt(this.voice_length.split(":")[1].replace("s", ""));
        playerTimer = new Timer();
        playerTimerTask = new TimerTask() {
            @Override
            public void run() {
                if (--playerLength < 0) {
                    Message message = new Message();
                    message.what = 0;
                    playerHandler.sendMessage(message);
                }
            }
        };
        playerTimer.schedule(playerTimerTask, 0, 1000);
        try {
            voicePlayer.setDataSource(path);
            voicePlayer.prepare();
            voicePlayer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void stopVoice () {
        button.setImageResource(R.drawable.play);
        voiceIsPlaying = false;
        voicePlayer.release();
        voicePlayer = null;
        SystemConstant.voicePlayer = null;
    }
}
